{% extends "index.html" %} 
<meta charset="UTF-8">

{% block index %}
{% csrf_token %}
<div class="x-nav">
	<span class="layui-breadcrumb">
        <a href="">知识共享</a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
      </a>
</div>
<div class="x-body">
	<div class="layui-row  ">
		<div class='layui-col-md6'>
	       <form class="layui-form layui-col-md12 x-so">
				<input class="layui-input" placeholder="文章名称" name="name" id="id_name">
		        <div class="layui-input-inline">
		            <select name="contrller" id ='id_article_type'>
		              <option value="">文章类型</option>
		              {% for item in articletype %}
		              	<option value="{{ item.id }}">{{ item.article_type_name }}</option>
		              {% endfor %}
		            </select>
		        </div>
		        {% if user.is_superuser %}
		        <div class="layui-input-inline">
		            <select name="contrller" id ='id_article_status'>
		              <option value="">文章状态</option>
		              <option value="0">新建</option>
		              <option value="1">发布</option>
		              <option value="2">撤回</option>
		            </select>
		        </div>
		        {% endif %}
	       </form>
	     </div>
	     <div class='UserTable'>
	       <button class="layui-btn"  data-type="reload"><i class="layui-icon" >&#xe615;</i></button>
	     </div>
     </div>

	{% if user.is_superuser %}
	<xblock>
		<div class="UserTable">
		    <button class="layui-btn" onclick="x_admin_show('添加资产','{% url 'articlecreate' %}')"><i class="layui-icon"></i>添加文章</button>
		</div>
	</xblock>
	{% endif %}
	
	<table class="layui-hide" id="article_table" lay-filter="article"></table> 
</div>
<script>
var csrf = $('input[name="csrfmiddlewaretoken"]').val();

layui.use('table', function(){
  var table = layui.table;
  //方法级渲染
  table.render({
    elem: '#article_table'
    ,url: "{% url 'articlelist' %}"
    ,method: 'post'
    ,where: {'csrfmiddlewaretoken':csrf}
    ,cols: [[
      {field:'article_name', title: '文章标题', sort: true, fixed: true,event:'article'}
      ,{field:'article_type', title: '文章类型', sort: true}
      ,{field:'article_updatetime', title: '更新时间', sort: true}
      ,{field:'article_status', title: '文章状态', sort: true}
      ,{field:'article_user', title: '编写人员',sort: true}
      ,{ title: '高级选项',fixed: 'right', align:'center', toolbar: '#barDemo'}
    ]]
    ,id: 'testReload'
    ,page: true
    ,limit: 15
    ,text:{
    	none:'暂无相关数据'
    }
  });
  
  table.on('tool(article)', function(obj){
    var data = obj.data.article_id;
    if(obj.event === 'article'){
    	url = "{% url 'articledetails' '0' %}";
    	url = url.replace(/0/,data);
    	x_admin_show('资产详情',url);
    }else if(obj.event === 'delete'){
    	url = "{% url 'articledelete' 'key' %}";
    	url = url.replace(/key/,data);
    	layer.confirm('该操作为敏感操作<br>点击确认，将删除该文章',function(index){
		    $.get(url,function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
    	})
    } else if(obj.event === 'revoke'){
    	url = "{% url 'articlerevoke' 'key' %}";
    	url = url.replace(/key/,data);
    	layer.confirm('该操作为敏感操作<br>点击确认，撤回该文章',function(index){
		    $.get(url,function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
    	})
    }else if(obj.event === 'public'){
    	url = "{% url 'articlepublic' 'key' %}";
    	url = url.replace(/key/,data);
    	layer.confirm('该操作为敏感操作<br>点击确认，发布该文章',function(index){
		    $.get(url,function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
    	})
    }else if(obj.event === 'edit'){
    	url = "{% url 'articleupdate' 'key' %}";
    	url = url.replace(/key/,data);
    	x_admin_show('资产详情',url);
    } else if(obj.event === 'detail'){
    	url = "{% url 'articledetails' '0' %}";
    	url = url.replace(/0/,data);
    	window.open(url);
    }
  });
  
  
  
  var $ = layui.$, active = {
	reload: function(){
      //执行重载
      table.reload('testReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
        	name: $('#id_name').val()
	    	,type: $("#id_article_type option:selected").val()
	    	,status: $("#id_article_status option:selected").val()
        }
      });
    }
  };
  $('.UserTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});

</script>
<script type="text/html" id="barDemo">
<div class="layui-btn-group">
	{% if user.is_superuser %}
		<a class="layui-btn layui-btn-xs" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>
  		<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
		{% verbatim %}
		{{# if(d.article_status=='发布'){ }}
			<a class="layui-btn layui-btn-xs" lay-event="revoke"><i class="layui-icon">&#xe6b2;</i>撤回</a>
		{{# } else { }}
			<a class="layui-btn layui-btn-xs" lay-event="public"><i class="layui-icon">&#xe609;</i>发布</a>
		{{# } }}
		{% endverbatim %}
	{% endif %}
	<a class="layui-btn layui-btn-xs" lay-event="detail"><i class="layui-icon">&#xe60a;</i>详情</a>
</div>
</script>
{% endblock %}